

exports.addUser = async (context) => {
  const { cloud } = context
  let a = await findWinning(cloud);

    let arr= [0,0,0,0,0,0,0,0]
    let list =  ["563233800548", "569393324376", "583865198133", "563367801510", "623218137783", "624563864086", "563402710408", "579027757892"];
    // [{id:"563233800548",nun:0}, 
    //             {id:"569393324376",nun:0}, 
    //             {id:"583865198133",nun:0}, 
    //             {id:"563367801510",nun:0}, 
    //             {id:"623218137783",nun:0}, 
    //             {id:"624563864086",nun:0}, 
    //             {id:"563402710408",nun:0}, 
    //             {id:"579027757892",nun:0}]
      for(let i= 0;i<a.length;i++ ){
          let itemId = JSON.parse(a[i].itemId)
          for(let j =0; j< itemId.length;j++){
              index = list.findIndex(item => {
                return item === itemId[j]   
              })
              arr[index]= arr[index]+1
          }

      }

   return arr
}


const findWinning = async (cloud) => {
  try {
    let sql = `SELECT itemId FROM shopping where gmt_create < '2020-10-14 00:00:00'`;
    let data = await cloudfn(cloud, sql);
    return data;
  } catch (err) {
    return err;
  }
};

const cloudfn = async (cloud, sql) => {
  let cbData = await cloud.dataspace.executeSql(sql, [])
  console.log("==============cbData==================", cbData)
  return cbData
}




